<?php
/////////////////////////////////////////////////////////////////////////////
// 这个文件是 网城创想多用户商城 项目的一部分
//
// Copyright (c) 2007 - 2008 www.shopnc.net 
//
// 要查看完整的版权信息和许可信息，请查看源代码中附带的 COPYRIGHT 文件，
// 或者访问 http://www.shopnc.net/ 获得详细信息。
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : message.manage.php   FILE_PATH : D:\webroot\mulitishop\system\message.manage.php
 * ....后台短消息管理
 *
 * @copyright Copyright (c) 2007 - 2007 www.shopnc.net 
 * @author 网城创想多用户商城开发团队 php_netproject@yahoo.com.cn
 * @package 
 * @subpackage 
 * @version Sat Sep 22 15:54:07 CST 2007
 */

require_once("../global.inc.php");

class SysMessageManage extends SystemFrameWork{
	/**
	 * 短消息对象
	 *
	 * @var obj
	 */
	var $obj_message;

	/**
	 * 验证对象
	 *
	 * @var obj
	 */
	var $obj_validate;
	/**
	 * 分页对象
	 *
	 * @var obj
	 */
	var $obj_page;
	/**
	 * 会员对象
	 *
	 * @var obj
	 */
	var $obj_member;
	
	function main(){
		/**
		 * 设置模板路径
		 */
		$this->setsubtemplates("system");

		/**
		 * 语言包
		 */
		$this->getlang("system_message");

		/**
		 * 创建短消息对象
		 */
		if (!is_object($this->obj_message)){
			require_once("message.class.php");
			$this->obj_message = new MessageClass();
		}

		/**
		 * 根据参数调用相应的方法
		 */
		switch ($this->_input['action']){
			case "add_message":
				$this->_add_message();
				break;
			case "save_message":
				$this->_save_message();
				break;
			case "view_message":
				$this->_view_message();
				break;
			case "del_message":
				$this->_del_message();
				break;
			default:
				$this->_list_message();
				break;
		}
	}
	
	/**
	 * 系统发件箱
	 */
	function _list_message(){
		/**
		 * 创建分页对象
		 */
		if (!is_object($this->obj_page)){
			require_once("commonpage.class.php");
			$this->obj_page = new CommonPage();
		}

		$this->obj_page->pagebarnum(20);    //每页20条记录
		//得到短消息列表
		$condition['system_state'] = 0;
		$condition['order'] = 1;
		$message_array = $this->obj_message->getMessage($condition,$this->obj_page,'*','message_system');
		$pagelist = $this->obj_page->show(1);      //分页显示
		
		
		/**
		 * 页面输出
		 */
		$this->output('message_array',$message_array);
		$this->output('pagelist',$pagelist);
		$this->output('system_message_object',$this->_b_config['system_message_object']);/*群发对象类型*/
		$this->showpage('sys_message.manage');
	}
	
	/**
	 * 新发短信息
	 */
	function _add_message(){
		
		$this->output('system_message_object',Common::showForm_Radio('system_message_object','',$this->_b_config['system_message_object'],'0','radio'," onclick='send_message(this.value);' "));
		$this->output('system_message_delete',Common::showForm_Radio('system_message_delete','',$this->_b_config['system_message_delete'],'1','radio'," onclick='send_delete(this.value);' "));
		$this->showpage('sys_message.add');		
	}
	
	/**
	 * 新发短信息
	 */
	function _save_message(){
		/**
		 * 创建验证对象
		 */
		if (!is_object($this->obj_validate)){
			require_once("commonvalidate.class.php");
			$this->obj_validate = new CommonValidate();
		}
		/**
		 * 创建会员对象
		 */
		if (!is_object($this->obj_member)){
			require_once ("member.class.php");
			$this->obj_member = new MemberClass();
		}
		
		$this->obj_validate->setValidate(array("input"=>$this->_input['title'],"require"=>"true","message"=>$this->_lang['errMessageTitle']));/*标题*/
		$this->obj_validate->setValidate(array("input"=>$this->_input['content'],"require"=>"true","message"=>$this->_lang['errMessageContent']));/*内容*/
		if ($this->_input['system_message_object'] == '2'){
			$this->obj_validate->setValidate(array("input"=>$this->_input['system_receive_name'],"require"=>"true","message"=>$this->_lang['errMessageReceiveName']));/*发送会员名*/
		}
		if ($this->_input['system_message_delete'] == '0'){
			$this->obj_validate->setValidate(array("input"=>$this->_input['end_date'],"require"=>"true","message"=>$this->_lang['errMessageEndDate']));/*有效时间截止日期*/
		}
		$error = $this->obj_validate->validate();
		if ($error != "" ){
			echo $error;
			exit;
		}
		
		/*发送短信息*/
		$input_param = array();
		$input_param['title'] = $this->_input['title'];/*标题*/
		$input_param['content'] = $this->_input['content'];/*内容*/
		$input_param['send_object'] = $this->_input['system_message_object'];/*发送对象类型*/
		$input_param['system_receive_name'] = $this->_input['system_receive_name'];/*发送会员名*/
		$message_system_id = $this->obj_message->addOneSystemMessage($input_param);
		
		if ($this->_input['system_message_object'] == 0){/*所有用户*/
			/*取所有会员的用户名*/
			$member_array = $this->obj_member->getMemberList("",$page,"login_name","simple");
		}elseif ($this->_input['system_message_object'] == 1){/*所有卖家*/
			$condition['member_type'] = 1;
			$member_array = $this->obj_member->getMemberList($condition,$page,"login_name","simple");
		}elseif ($this->_input['system_message_object'] == 2){
			/*判断用户是否存在*/
			$this->_input['system_receive_name'] = str_replace('，',',',$this->_input['system_receive_name']);
			$name_array = @explode(',',trim($this->_input['system_receive_name']));
			if (is_array($name_array)){
				foreach ($name_array as $v){
					$condition = '';
					$condition['member_name'] = $v;
					$flag = $this->obj_member->checkMemberExist($condition);
					if (false != $flag){
						$member_array[]['login_name'] = $v;
					}
				}
			}
		}
		/*群发短信息*/
		/*有效时间截止日期*/
		if ($this->_input['system_message_delete'] == '0'){
			$time = @explode('-',$this->_input['end_date']);
			$time = @mktime(0,0,0,$time[1],$time[2],$time[0]);
		}
		
		if (is_array($member_array)){
			foreach ($member_array as $v){
				$input_param = array();
				$input_param["member_name"] = 0;/*0为系统发信*/
				$input_param["txtReceive_name"] = $v['login_name'];/*接收人用户名*/
				$input_param["txtTitle"] = $this->_input['title'];/*短信息标题*/
				$input_param["txtContent"] = $message_system_id;/*关联群发表中内容ID*/
				$input_param["txtEndDate"] = $time;/*有效时间截止日期*/
				$input_param["rdoIsallowdel"] = $this->_input['system_message_delete'];/*是否允许删除*/
				$this->obj_message->addMessage($input_param);
			}
		}
		echo 'ok';
		exit;
	}
	
	/**
	 * 查看短信息内容
	 */
	function _view_message(){
		
		/*取短信息内容*/
		$condition['message_system_id'] = $this->_input['message_system_id'];
		$message_array = $this->obj_message->getOneMessage($condition,'*','message_system');
		
		/**
		 * 页面输出
		 */
		$this->output('message_array',$message_array);
		$this->output('system_message_object',$this->_b_config['system_message_object']);/*群发对象类型*/
		$this->showpage('sys_message.view');
	}
	
	/**
	 * 删除短信息内容
	 */
	function _del_message(){
		
		if (is_array($this->_input['message_system_id'])){
			$this->obj_message->delSystemMessage($this->_input['message_system_id']);
		}		
		//$this->obj_message->delMessage($this->_input['message_system_id'],'','message_system','message_system_id');
		echo 'ok';
		exit;
	}
}

$message_manage = new SysMessageManage();
$message_manage->main();
unset($message_manage);
?>